import Util from '../../utils/util.js';
import {getShopDetail} from "../../api/shop";

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shop: {
      title: "中南食堂",
      contact_phone: ""
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;

    wx.showLoading({
      title: "正在加载..."
    })
    this.setData({...options})
    Util.chekWxLogin().then(res => {
      that.setData({
        userInfo: res.userInfo,
      })

      if (!this.data.sid) {
        Util.Tips({title: "参数错误"}, {
          tab: 1,
          url: "/pages/index/index"
        })
        return false;
      }

      getShopDetail(that.data.sid).then(res => {
        that.setData({
          shop: res.data
        })
      }).catch(errmsg => {
        Util.Tips({title: errmsg})
      }).finally(() => {
        wx.hideLoading()
      })

      // that.onLoadTabbar();
    }).catch(errmsg => {
      Util.Tips({title: errmsg})
    }).finally(() => {
      wx.hideLoading();
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  previewImage(e) {//单张图片预览
    let src = e.currentTarget.dataset.src
    if (Util.isEmpty(src))
      return Util.Tips({title: "图片不存在", hideMask: true})

    let images = []
    if (Util.isEmpty(src)) return;
    src = Util.previewCdnImage(src, 1080, 0);

    images.push(src)
    wx.previewImage({
      current: src,
      urls: images,
    });
  },

  previewMutiImage: function (event) {
    let imgList = this.data.shop.pictures || [];
    if (imgList.length == 0)
      return;

    const key = event.currentTarget.dataset.key
    let src = "";
    if (!Util.isEmpty(imgList[key]))
      src = Util.previewCdnImage(imgList[key].path, 750, 0, 1)

    const urls = [];
    for (let i = 0; i < imgList.length; i++) {
      urls.push(Util.previewCdnImage(imgList[i]['path'], 750, 0, 1));
    }
    if (urls) {
      wx.previewImage({
        current: src,
        urls: urls,
      });
    }

  },

  makePhoneCall(e) {
    const mobile = e.currentTarget.dataset.mobile;
    Util.callPhone(mobile)
  },

  openMap(e) {
    const that = this;
    const lat = 39.9053936;
    const lng = 116.3913447;
    wx.openLocation({
      latitude: parseFloat(lat),
      longitude: parseFloat(lng),
      name: this.data.shop.title || '当前店铺',
      scale: 17
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})